﻿using EasyERP_DAL;
using EasyERP_DAL.Base;
using EasyERP_Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComponentFactory.Krypton.Toolkit;
using System.Windows.Forms;

namespace EasyERP_BLL.Base
{
    /// <summary>
    /// 主代码业务逻辑类
    /// </summary>
    public class B_Major_BLL
    {
        /// <summary>
        /// 声明只读主代码数据访问类对象
        /// </summary>
        private readonly B_Major_DAL Major_DAL = new B_Major_DAL();

        #region 查询主代码信息
        /// <summary>
        /// 查询主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回查询表</returns>
        public DataTable Get_B_Major(B_Major model)
        {
            try
            {
                return Major_DAL.Get_B_Major(model);
            }
            catch (Exception ex)
            {
                B_Message_BLL.MessageCaveat(ex);
                return null;
            }
        }
        #endregion

        #region 添加主代码信息
        /// <summary>
        /// 添加主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Insert_B_Major(B_Major model)
        {
            if (string.IsNullOrWhiteSpace(model.major_cd))      //主代码编号为空
            {
                B_Message_BLL.MessageCaveat("2001");        //弹出提示框
                return false;
            }

            if (string.IsNullOrWhiteSpace(model.major_nm))      //主代码名称为空
            {
                B_Message_BLL.MessageCaveat("2002");        //弹出提示框
                return false;
            }

            //保存前验证数据是否存在
            if (Major_DAL.Exists(model))      //存在
            {
                B_Message_BLL.MessageCaveat("1001");        //弹出提示框
                return false;
            }

            try
            {
                return Major_DAL.Insert_B_Major(model);
            }
            catch (Exception ex)
            {
                B_Message_BLL.MessageCaveat(ex);
                return false;
            }
        }
        #endregion

        #region 删除主代码信息
        /// <summary>
        /// 删除主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Delete_B_Major(B_Major model)
        {
            if (string.IsNullOrWhiteSpace(model.major_cd))      //主代码编号为空
            {
                B_Message_BLL.MessageCaveat("2001");        //弹出提示框
                return false;
            }

            //删除前验证数据是否存在
            if (Major_DAL.Exists(model))     //不存在
            {
                //查询是否有对应的子代码数据
                if (ExistsMinor(model))                       //有数据
                {
                    B_Message_BLL.MessageCaveat("1006");
                    return false;
                }

                try
                {
                    return Major_DAL.Delete_B_Major(model);
                }
                catch (Exception ex)
                {
                    B_Message_BLL.MessageCaveat(ex);
                    return false;
                }
            }
            B_Message_BLL.MessageCaveat("1002");        //弹出提示框
            return false;

        }
        #endregion

        #region 修改主代码信息
        /// <summary>
        /// 修改主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Updata_B_Major(B_Major model)
        {
            if (string.IsNullOrWhiteSpace(model.major_cd))      //主代码编号为空
            {
                B_Message_BLL.MessageCaveat("2001");        //弹出提示框
                return false;
            }

            if (string.IsNullOrWhiteSpace(model.major_nm))      //主代码名称为空
            {
                B_Message_BLL.MessageCaveat("2002");        //弹出提示框
                return false;
            }

            //修改前验证数据是否存在
            if (Major_DAL.Exists(model))     //不存在
            {
                try
                {
                    return Major_DAL.Update_B_Major(model);
                }
                catch (Exception ex)
                {
                    B_Message_BLL.MessageCaveat(ex);
                    return false;
                }
            }
            B_Message_BLL.MessageCaveat("1003");        //弹出提示框
            return false;
        }
        #endregion

        #region 删除主代码前查询是否有对应的子代码
        /// <summary>
        /// 删除主代码前查询是否有对应的子代码
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：存在；返回False：不存在</returns>
        public bool ExistsMinor(B_Major model)
        {
            try
            {
                return Major_DAL.ExistsMinor(model);
            }
            catch (Exception ex)
            {
                B_Message_BLL.MessageCaveat(ex);
                return false;
            }
        }
        #endregion
    }
}
